
package com.hlkj.pay.util;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * @author HlpayTeam
 * @date 2024/12/13 17:16
 */
public class HttpRequestUtil {

    /**
     * body 参数解析
     * 
     * @param request
     * @return
     */
    public static String requestBodyParams(HttpServletRequest request) {
        try {
            // 读取请求体中的内容
            StringBuilder stringBuilder = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
            }
            // 获取完整的 JSON 字符串
            return stringBuilder.toString();
        }
        catch (Exception e) {
            return null;
        }
    }

    /**
     * form 表单参数解析
     * 
     * @param request
     * @return
     */
    public static Map<String, String> requestFormParams(HttpServletRequest request) {
        Map<String, String> parameter = new HashMap<>();
        Map<String, String[]> parameterMap = request.getParameterMap();
        parameterMap.forEach((key, value) -> {
            parameter.put(key, value[0]);
        });
        return parameter;
    }
}
